package com.zy.core.filter;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zy.pro.shop.model.ShopUser;
import com.zy.pro.system.model.Employee;
import com.zy.pro.user.model.User;
import com.zy.utils.SessionUtils;

public class EmployeeFilter implements Filter {

	public void destroy() {
		// TODO Auto-generated method stub

	}
	
	public void doFilter(ServletRequest req, ServletResponse res,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) req;
		
		// 不过滤的uri
		String[] notFilter = new String[] { 
				"/control/layoutController/displayIndex.do",
				"/control/layoutController/displayShop.do",
				"/control/layoutController/displayCase.do",
				"/control/layoutController/displayShopDetail.do",
				"/control/layoutController/displayCaseDetail.do",
				"/control/layoutController/ajaxShopCaseList.do",
				"/control/layoutController/ajaxShopCommentList.do",
				"/control/layoutController/ajaxCaseCommentList.do",
				"/control/layoutController/displayUserLogin.do",
				"/control/layoutController/displayShopLogin.do",
				"/control/layoutController/displayUserRegister.do",
				"/control/layoutController/search.do",
				"/control/layoutController/displayNoticeList.do",
				"/control/layoutController/displayNoticeDetail.do",
				"/control/layoutController/displayContact.do",
				
				"/control/user/userController/login.do",
				"/control/user/userController/userRegister.do",
				"/control/user/userController/checkOldPassword.do",
				"/control/user/userController/activateAccount.do",
				"/control/user/userController/findPassword.do",
				//跳转用户找回密码页面
				"/control/layoutController/displayUserFindPassword.do",
				
				
				"/control/calendar/calendarController/calendarList.do",
				
				"/control/shop/shopUserController/login.do",
				
				"/control/system/employeeController/uploadFile.do",
				
				"/supply/globalController/findCheck.do",
				"/supply/globalController/findCheckEmail.do",
				"/supply/globalController/findCheckUserName.do",
				
				//"/control/layoutController/toMail.do",
				
		};
		
		// 用户执行的uri
		String[] notUserFilter = new String[] { 
				"/control/calendar/calendarController/calendarList.do",
				"/control/letter/letterController/userAddLetter.do",
				"/control/letter/letterController/displayLetterDetail.do",
				"/control/letter/letterController/displayReplyLetter.do",
				"/control/letter/letterController/replyLetter.do",
				"/control/letter/letterController/userSentLetterList.do",
				"/control/letter/letterController/userUnreadLetterList.do",
				"/control/letter/letterController/userReadLetterList.do",
				"/control/letter/letterController/checkOrderByUserAndShop.do",
				"/control/order/orderItemController/userAddOrder.do",
				"/control/order/orderItemController/userOrderList.do",
				"/control/order/orderItemController/userOrderExecutionList.do",
				"/control/order/orderItemController/userOrderCompleteList.do",
				"/control/order/orderItemController/userDeletedOrder.do",
				"/control/order/orderItemController/userConfirmOrder.do",
				"/control/order/orderItemController/userApplyRefund.do",
				"/control/order/orderItemController/userConfirmRefund.do",
				"/control/order/orderItemController/displayOrderDetail.do",
				"/control/order/commentController/userAddComment.do",
				"/control/ec/expressCheckoutController/doEC.do",
				"/control/ec/returnController/doReturn.do",
				
				"/control/user/userController/displayUser.do",
				"/control/user/userController/displayUserInfo.do",
				"/control/user/userController/displayEdit.do",
				"/control/user/userController/edit.do",
				"/control/user/userController/displayEditUserCode.do",
				"/control/user/userController/checkOldPassword.do",
				"/control/user/userController/editUserCode.do",
				
				
				"/control/layoutController/loginOut.do",
		};
		
		// 卖家执行的uri
		String[] notShopFilter = new String[] { 
				"/control/calendar/calendarController/displayCalendar.do",
				"/control/calendar/calendarController/addCalendar.do",
				"/control/calendar/calendarController/deleteCalendar.do",
				"/control/calendar/calendarController/calendarList.do",
				"/control/letter/letterController/displayLetterDetail.do",
				"/control/letter/letterController/displayReplyLetter.do",
				"/control/letter/letterController/replyLetter.do",
				"/control/letter/letterController/shopSentLetterList.do",
				"/control/letter/letterController/shopUnreadLetterList.do",
				"/control/letter/letterController/shopReadLetterList.do",
				"/control/letter/letterController/checkOrderByUserAndShop.do",
				"/control/order/orderItemController/shopOrderList.do",
				"/control/order/orderItemController/shopOrderExecutionList.do",
				"/control/order/orderItemController/shopOrderCompleteList.do",
				"/control/order/orderItemController/shopDeletedOrder.do",
				"/control/order/orderItemController/editOrderMoney.do",
				"/control/order/orderItemController/setOrderThisMoney.do",
				"/control/order/orderItemController/displayOrderDetail.do",
				"/control/order/orderItemController/shopRefund.do",
				"/control/order/commentController/shopCommentList.do",
				"/control/order/commentController/shopDeleteComment.do",
				"/control/ec/expressCheckoutController/doEC.do",
				"/control/ec/returnController/doReturn.do",
				"/control/shop/casesController/list.do",
				"/control/shop/casesController/displayAdd.do",
				"/control/shop/casesController/add.do",
				"/control/shop/casesController/displayEdit.do",
				"/control/shop/casesController/edit.do",
				"/control/shop/casesController/editStatus.do",
				"/control/shop/shopController/displayShop.do",
				"/control/shop/shopController/displayShopEdit.do",
				"/control/shop/shopController/edit.do",
				"/control/shop/shopController/displayShopBond.do",
				"/control/shop/shopUserController/displaySeller.do",
				
				"/control/shop/shopController/displayEditShopUserCode.do",
				"/control/shop/shopController/checkOldPassword.do",
				"/control/shop/shopController/editShopUserCode.do",
				
				"/supply/globalController/findCheck.do",
				
				"/control/layoutController/loginOut.do",
		};
		
		// 请求的uri
		String uri = request.getRequestURI();
		if (uri.indexOf(".do") != -1) {
			boolean doFilter = true;
			for (String s : notFilter) {
				if (uri.indexOf(s) != -1) {
					// 如果uri中包含不过滤的uri，则不进行过滤
					doFilter = false;
					break;
				}
			}
			if (doFilter) {
				//从session中获取店家
		    	ShopUser suSession = null;
		    	//从session中获取业主
		    	User userSession = null;
		    	String roleType = SessionUtils.getRoleType(request);
		    	if("1".equals(roleType)){
		    		userSession = SessionUtils.getUser(request);
		    		if(userSession != null){
		    			for (String s : notUserFilter) {
		    				if (uri.indexOf(s) != -1) {
		    					// 如果uri中包含不过滤的uri，则不进行过滤
		    					doFilter = false;
		    					break;
		    				}
		    			}
		    			if (doFilter) {
		    				toHome(request,res,"/control/layoutController/displayUserLogin.do");
		    			}else{
		    				chain.doFilter(req, res);
		    			}
		    		}
		    	}else if("2".equals(roleType)){
		    		suSession = SessionUtils.getShopUser(request);
		    		if(suSession != null){
		    			for (String s : notShopFilter) {
		    				if (uri.indexOf(s) != -1) {
		    					// 如果uri中包含不过滤的uri，则不进行过滤
		    					doFilter = false;
		    					break;
		    				}
		    			}
		    			if (doFilter) {
		    				toHome(request,res,"/control/layoutController/displayShopLogin.do");
		    			}else{
		    				chain.doFilter(req, res);
		    			}
		    		}
		    	}else{
		    		toHome(request,res,"");
		    		//chain.doFilter(req, res);
		    	}
			} else {
				chain.doFilter(req, res);
			}
		} else {
			
			/*if (uri.indexOf("/control/jsp/mail.jsp") != -1) {
				chain.doFilter(req, res);
			}else{
				toHome(request,res,"");
			}*/
			toHome(request,res,"");
			//chain.doFilter(req, res);
		}
		//chain.doFilter(req, res);
		
	}

	
	public void toHome(HttpServletRequest request,ServletResponse res, String url) throws IOException{
		HttpServletResponse response = (HttpServletResponse) res;
		try {
			request.setCharacterEncoding("UTF-8");
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			StringBuilder builder = new StringBuilder();
			builder.append("<script type=\"text/javascript\">");
//			builder.append("alert('网页过期，请重新登录！');");
			builder.append("window.top.location.href='");
			builder.append(request.getScheme());
			builder.append("://");
			builder.append(request.getServerName());
			builder.append(":");
			builder.append(request.getServerPort());
			builder.append(request.getContextPath());
			builder.append(url);
//			builder.append("/system/logonController/login.do");
			builder.append("';");
			builder.append("</script>");
			out.print(builder.toString());
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}
	
	/*public void doFilter(ServletRequest req, ServletResponse res,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) req;
		chain.doFilter(req, res);
//		Employee employee = SessionUtils.getUser(request);
		if(employee==null){
			// 如果session中不存在登录者实体，则弹出框提示重新登录
			// 设置request和response的字符集，防止乱码
			HttpServletResponse response = (HttpServletResponse) res;
			request.setCharacterEncoding("UTF-8");
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			StringBuilder builder = new StringBuilder();
			builder.append("<script type=\"text/javascript\">");
//			builder.append("alert('网页过期，请重新登录！');");
			builder.append("window.top.location.href='");
			builder.append(request.getScheme());
			builder.append("://");
			builder.append(request.getServerName());
			builder.append(":");
			builder.append(request.getServerPort());
			builder.append(request.getContextPath());
//			builder.append("/system/logonController/login.do");
			builder.append("';");
			builder.append("</script>");
			out.print(builder.toString());
			
//			String tagetLoginPath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+ "/system/logonController/login.do";
//			response.sendRedirect(tagetLoginPath);
//			request.getRequestDispatcher("/system/logonController/login.do").forward(request, response);
			
		}else{
			chain.doFilter(req, res);
		}
	}*/

	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}

}
